#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import cv2
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
    import elsLibs.ShotInfoFromFile as elssif
    reload(elssif)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckVideoFrameRange(CheckBase):
    def __init__(self):
        super(CheckVideoFrameRange, self).__init__()
        pass

    def init_file(self):
        scene_name = mu.sceneName()
        sif = elssif.ShotInfoFromFile(scene_name)
        self.seqshot = sif.get_seqshot

    def check(self):
        current_file = mu.sceneName()
        if not current_file:
            result_log = u'Error: 当前文件未保存!'
            return result_log
        if current_file.startswith('els_'):
            result_log = u'Error: 当前文件非els项目镜头规则!'
            return result_log
        try:
            self.init_file()
        except:
            result_log = u'Error:当前文件不符合ELS项目资产文件命名规则'
            return result_log

        mov_file = current_file[0:-3] + '.mov'
        if os.path.exists(mov_file):
            # 使用cv2库来查询视频总帧数
            mov = cv2.VideoCapture(mov_file)
            frames_count = mov.get(cv2.CAP_PROP_FRAME_COUNT)
            mov.release()
            # 查询maya内时间线帧范围
            ast = cmds.playbackOptions(q=True, ast=True)
            aet = cmds.playbackOptions(q=True, aet=True)
            if not ((aet - ast + 1) == frames_count):
                result_log = u'Error:动画帧数与拍屏文件帧数不一致'
            else:
                result_log = u'Succeed:没有问题\n'
        else:
            result_log = u'Error:没有找到符合当前文件命名的视频'

        return result_log

    def fix(self):
        pass